<?php
/**
 * Created by PhpStorm.
 * User: jim
 * Date: 2017-01-09
 * Time: 16:39
 */

namespace App\Http\Controllers;


use App\Models\Order;

class Data
{
    /**
     *商户收入统计
     *@$action 统计模式：m:月 d:日 s:小时
     * @return $income
     */

    public $sum = [];
    public $count = [];
    public $order = [];
    public $select = [];
    public $action = 'm';

    public function select($action)
    {
        $y = date('Y', time());
        $m = date('m', time());
        $d = date('d', time());
        $this->action = $action;
        switch ($this->action) {
            case "m":
                for ($i = 1; $i <= 12; $i++) {
                    $date = $y . '-' . sprintf('%02d', $i) . "-00 00:00:00";
                    $date = strtotime($date) * 1000;
                    $sum = Order::where([['state', '5'], ['status', '>=', '4'], ['status', '<=', '5']])->whereYear('created_at', $y)->whereMonth('created_at', $i)->sum('realpay');
                    $count = Order::where([['state', '5'], ['status', '>=', '4'], ['status', '<=', '5']])->whereYear('created_at', $y)->whereMonth('created_at', $i)->count('*');
                    $order = Order::whereYear('created_at', $y)->whereMonth('created_at', $i)->count('*');
                    $this->order[] = [$date, $order];      //每月订单总数
                    $this->count[] = [$date, $count];      //每月有效订单总数
                    $this->sum[] = [$date, $sum];          //每月有效订单实付总金额
                }
                return $this;
                break;
            case 'd':
                for ($i = 1; $i <= date('t', $m); $i++) {
                    $date = $y . '-' . $m . '-' . sprintf('%02d', $i) . " 00:00:00";
                    $date = strtotime($date) * 1000;
                    $d = $y . '-' . $m . '-' . sprintf('%02d', $i);
                    $sum = Order::where([['state', '5'], ['status', '>=', '4'], ['status', '<=', '5']])->where('created_at', 'like', "%$d%")->sum('realpay');
                    $count = Order::where([['state', '5'], ['status', '>=', '4'], ['status', '<=', '5']])->where('created_at', 'like', "%$d%")->count('*');
                    $order = Order::where('created_at', 'like', "%$d%")->count('*');
                    $this->order[] = [$date, $order];      //每日有单总数
                    $this->count[] = [$date, $count];      //每日有效订单总数
                    $this->sum[] = [$date, $sum];          //每日有效订单实付总金额
                }
                return $this;
                break;
            case 'h':
                for ($i = 0; $i < 24; $i++) {
                    $date = $y . '-' . $m . '-' . $d . " " . sprintf('%02d', $i) . ":00";
                    $date = strtotime($date) * 1000 + 8 * 60 * 60 * 1000;
                    $h = $y . '-' . $m . '-' . $d . ' ' . sprintf('%02d', $i);
                    $sum = Order::where([['state', '5'], ['status', '>=', '4'], ['status', '<=', '5']])->where('created_at', 'like', "%$h%")->sum('realpay');
                    $count = Order::where([['state', '5'], ['status', '>=', '4'], ['status', '<=', '5']])->where('created_at', 'like', "%$h%")->count('*');
                    $order = Order::where('created_at', 'like', "%$h%")->count('*');
                    $this->order[] = [$date, $order];      //每时订单总数
                    $this->count[] = [$date, $count];      //每时有效订单总数
                    $this->sum[] = [$date, $sum];          //每时有效订单实付总金额
                }
                return $this;
                break;
        }

    }

    public function select_s($action)
    {
        $id=session('storelist')->id;
        $y = date('Y', time());
        $m = date('m', time());
        $d = date('d', time());
        $this->action = $action;
        switch ($this->action) {
            case "m":
                for ($i = 1; $i <= 12; $i++) {
                    $date = $y . '-' . sprintf('%02d', $i) . "-00 00:00:00";
                    $date = strtotime($date) * 1000;
                    $sum = Order::where([['state', '5'], ['status', '>=', '4'], ['status', '<=', '5'], ['store_id', $id]])->whereYear('created_at', $y)->whereMonth('created_at', $i)->sum('realpay');
                    $count = Order::where([['state', '5'], ['status', '>=', '4'], ['status', '<=', '5'], ['store_id', $id]])->whereYear('created_at', $y)->whereMonth('created_at', $i)->count('*');
                    $order = Order::where('store_id', $id)->whereYear('created_at', $y)->whereMonth('created_at', $i)->count('*');
                    $this->order[] = [$date, $order];      //每月订单总数
                    $this->count[] = [$date, $count];      //每月有效订单总数
                    $this->sum[] = [$date, $sum];          //每月有效订单实付总金额
                }
                return $this;
                break;
            case 'd':
                for ($i = 1; $i <= date('t', $m); $i++) {
                    $date = $y . '-' . $m . '-' . sprintf('%02d', $i) . " 00:00:00";
                    $date = strtotime($date) * 1000;
                    $d = $y . '-' . $m . '-' . sprintf('%02d', $i);
                    $sum = Order::where([['state', '5'], ['store_id', $id], ['status', '>=', '4'], ['status', '<=', '5']])->where('created_at', 'like', "%$d%")->sum('realpay');
                    $count = Order::where([['state', '5'], ['store_id', $id], ['status', '>=', '4'], ['status', '<=', '5']])->where('created_at', 'like', "%$d%")->count('*');
                    $order = Order::where('store_id', $id)->where('created_at', 'like', "%$d%")->count('*');
                    $this->order[] = [$date, $order];      //每日有单总数
                    $this->count[] = [$date, $count];      //每日有效订单总数
                    $this->sum[] = [$date, $sum];          //每日有效订单实付总金额
                }
                return $this;
                break;
            case 'h':
                for ($i = 0; $i < 24; $i++) {
                    $date = $y . '-' . $m . '-' . $d . " " . sprintf('%02d', $i) . ":00";
                    $date = strtotime($date) * 1000 + 8 * 60 * 60 * 1000;
                    $h = $y . '-' . $m . '-' . $d . ' ' . sprintf('%02d', $i);
                    $sum = Order::where([['state', '5'], ['store_id', $id], ['status', '>=', '4'], ['status', '<=', '5']])->where('created_at', 'like', "%$h%")->sum('realpay');
                    $count = Order::where([['state', '5'], ['store_id', $id], ['status', '>=', '4'], ['status', '<=', '5']])->where('created_at', 'like', "%$h%")->count('*');
                    $order = Order::where('store_id', $id)->where('created_at', 'like', "%$h%")->count('*');
                    $this->order[] = [$date, $order];      //每时订单总数
                    $this->count[] = [$date, $count];      //每时有效订单总数
                    $this->sum[] = [$date, $sum];          //每时有效订单实付总金额
                }
                return $this;
                break;
        }

    }

    /** 统计日期内的订单总数 */
    public function order()
    {
        $this->select = $this->order;
        return $this;
    }

    /** 统计日期内有效订单总数 */
    public function count()
    {
        $this->select = $this->count;
        return $this;
    }

    /** 统计日期内有效订单实付总金额 */
    public function sum()
    {
        $this->select = $this->sum;
        return $this;
    }


    /** 统计所有筛选日期内的数据总和 并返回 Int */
    public function total()
    {
        $res = "";
        foreach ($this->select as $v) {
            $res += $v[1];
        }
        return $res;
    }

    /** 统计所有筛选日期内的单条数据 并返回 Array */
    public function list()
    {
        return $this->select;
    }

}